<?
	include_once ("../include/conn.php");
	include_once ('config.php');

	/**************************************** 
	****************更新用户 真实联系信息*****************
	******************************************/


	//开始数据处理
	$openid       = $_POST['openid'];
	$unionid      = $_POST['unionid'];
	$contact_name = $_POST['contact_name'];
	$contact_tel  = $_POST['contact_tel'];

	$company      = $_POST['company'];
	$size         = $_POST['size'];

	$province_name  = $_POST['province_name'];   //省
	$province_id    = $_POST['province_id'];     //省编号
	$city_name      = $_POST['city_name'];       //市
	$city_id        = $_POST['city_id'];         //市编号
	$area_name      = $_POST['area_name'];       //县区
	$area_id        = $_POST['area_id'];         //县区编号
	$scope_name     = $_POST['scope_name'];      //园区
	$scope_id       = $_POST['scope_id'];        //园区id
	$submitFullAddr = $_POST['submitFullAddr'];  //完整地址串
	
	#默认更新语句只更新联系人和电话
	#下面判断如果有发送公司来，就重置sql，一起处理公司信息
	$cmd = " update {$TABLE_C}user set contact_name = '$contact_name', contact_tel = '$contact_tel' where unionid='$unionid' ";

	#如果有发送compnay过来，说明是企业用户
	if($company){
		$company_id = null; //用于保存到用户表的公司id,通过传过来的compnay公司名称，查找数据库，得到id
		
		# 查找是否有这间公司,为防止出乱子，只读取一条公司
		$cmdC = "select * from {$TABLE_C}company where title ='$company' limit 1";

		# 如果有这间公司，得到id
		if($mydb->num_rows($cmdC)>0){
			$datas= $mydb->fetch_assoc($cmdC);
			$company_id = $datas['id'];
		}else{
			#没有就新建这家公司，并得得到id
			$cmd_new_company = "
			insert into {$TABLE_C}company 
			    ( title, size, scope, scope_id, area, area_id, city, city_id, province, province_id, full_addr ) 
			values( '$company', '$size', '$scope_name', $scope_id, '$area_name', '$area_id', '$city_name', '$city_id', '$province_name', '$province_id', '$submitFullAddr' )";
			$mydb->query($cmd_new_company);
			$datas= $mydb->fetch_assoc($cmdC);
			$company_id = $datas['id'];
		}
		
		// 如果有发送联系人和联系电话，说明没有进个人问卷补充过，则在这里一起更新
		if($contact_name && $contact_tel) {
			$sql_link = "contact_name = '$contact_name', contact_tel = '$contact_tel',";
		}
		
		// 组合更新语句 
		$cmd = " update {$TABLE_C}user set  $sql_link company = $company_id where unionid='$unionid' ";
	}


	// echo "公司id：$company_id";
	// echo "*************************************";
	// exit;
	

	#更新到数据库
	if($mydb->query($cmd)){
		$results = array('code' => 0,'msg'=>'更新用户信息成功！');//
	}else{
		$results = array('code' => 1,'msg'=>'更新失败了！！？？？');//
	}


	// //////////////////输出数据////////////////////////////////
	echo json_encode($results);
	mysql_close();
?>